package cn.factorybox.common.entity;

import org.springframework.http.HttpStatus;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 自定义返回体
 * @author flypig
 * @version 1.0
 * @date 2020-03-04 10:40
 */
public class Result extends HashMap<String,Object> {

    private static final long serialVersionUID = 1L;

    public final static Integer RESULT_CODE_101=101;
    public final static String RESULT_CODE_101_TEXT="数据已存在";

    public Result() {
        put("code", Constant.SUCCEED_CODE);
        put("flag", true);
//        System.out.println("返回成功.........");
    }

    public Result(Object data, String message) {
        put("code", Constant.SUCCEED_CODE);
        put("flag", true);
        put("data", data);
        put("message",message);
    }

    public static Result error(){
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知异常，请联系管理员");
    }

    public static Result error(String message) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
    }

    public static Result error(int code, String message) {
        Result r = new Result();
        r.put("code", code);
        r.put("flag",false);
        r.put("message", message);
        return r;
    }

    public static Result ok() {
        return new Result();
    }

    public static Result ok(String message) {
        Result r = new Result();
        r.put("message", message);
        return r;
    }

    public static Result ok(Map<String, Object> map) {
        Result r = new Result();
        r.putAll(map);
        return r;
    }

    public static Result ok(Map<String, Object> map,String message) {
        Result r = new Result();
        r.putAll(map);
        r.put("message", message);
        return r;
    }

    public static Result ok(int nums) {
        Result r = new Result();
        r.put("message", nums);
        return r;
    }

    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public static Result success(Map<String, Object> map,String message) {
        Result r = new Result();
        r.put("data",map);
        r.put("message", message);
        return r;
    }
}
